package com.douban.crawl.servlet;

import com.douban.crawl.db.BookDao;
import com.douban.crawl.db.MovieDao;
import com.douban.crawl.db.MusicDao;
import com.douban.crawl.model.Book;
import com.douban.crawl.model.Movie;
import com.douban.crawl.model.Music;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;


public class MainServlet extends HttpServlet {

    private MovieDao movieDao = new MovieDao();
    private MusicDao musicDao = new MusicDao();
    private BookDao bookDao = new BookDao();

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        List<Movie> movieList = movieDao.getList(5);
        List<Music> musicList = musicDao.getList(5);
        List<Book> bookList = bookDao.getList(5);
        req.setAttribute("movieList", movieList);
        req.setAttribute("musicList", musicList);
        req.setAttribute("bookList", bookList);

        List<Movie> hotMovieList = movieDao.getList(10);
        req.setAttribute("hotMovieList", hotMovieList);
        List<Music> hotMusicList = musicDao.getList(10);
        req.setAttribute("hotMusicList", hotMusicList);
        List<Book> hotBookList = bookDao.getList(10);
        req.setAttribute("hotBookList", hotBookList);

        req.getRequestDispatcher("/main.jsp").forward(req, resp);
    }
}
